package com.novax.ex.activity.provider.controller;

import cn.hutool.core.bean.BeanUtil;
import com.novax.ex.activity.infrastructure.entity.MallConfig;
import com.novax.ex.activity.open.api.MallConfigApi;
import com.novax.ex.activity.open.api.inner.MallConfigInnerApi;
import com.novax.ex.activity.open.model.query.MallConfigQuery;
import com.novax.ex.activity.open.model.request.MallConfigRequest;
import com.novax.ex.activity.open.model.response.MallConfigResponse;
import com.novax.ex.activity.provider.service.MallConfigService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.constant.StatusCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:参与费用配置
 *
 * @author Deucalion@novax.email
 * @date 2023/2/21 17:21
 */
@Slf4j
@RestController
public class MallConfigController extends BaseActivityController implements MallConfigInnerApi, MallConfigApi {
    @Resource
    private MallConfigService mallConfigService;

    @Override
    public ReturnResult add(String language, Long adminId, MallConfigRequest request,BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(BigDecimal.ZERO.compareTo(request.getMinPrice()) >= 0){
            return ReturnResult.fail(getInternational(language, InternationalCode.PARTICIPATION_FEE_NON_VANISHING));
        }
        // 数据查重
        MallConfig repetitive = mallConfigService.findByCurrency(request.getCurrency());
        if(null != repetitive){
            return ReturnResult.fail(getInternational(language, InternationalCode.DUPLICATED_DATA));
        }
        MallConfig entity = BeanUtil.copyProperties(request, MallConfig.class);
        entity.setId(null);
        request.setCurrency(request.getCurrency().toLowerCase());
        request.setMinPrice(request.getMinPrice().setScale(2, BigDecimal.ROUND_DOWN));
        entity.setCreator(adminId);
        boolean result = mallConfigService.addSelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult modify(String language, Long adminId, MallConfigRequest request, BindingResult bindingResult) {
        if(null == request.getId()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(BigDecimal.ZERO.compareTo(request.getMinPrice()) >= 0){
            return ReturnResult.fail(getInternational(language, InternationalCode.PARTICIPATION_FEE_NON_VANISHING));
        }
        MallConfig old = mallConfigService.find(request.getId());
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallConfig entity = BeanUtil.copyProperties(request, MallConfig.class);
        entity.setCurrency(null);
        request.setMinPrice(request.getMinPrice().setScale(2, BigDecimal.ROUND_DOWN));
        entity.setUpdater(adminId);
        boolean result = mallConfigService.modifySelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<PageUtil<MallConfigResponse>> list(String language, MallConfigQuery query) {
        PageUtil<MallConfigResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        pages.setCondition(condition);
        mallConfigService.findPage(pages,MallConfigResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), pages);
    }

    @Override
    public ReturnResult<MallConfigResponse> detail(Long id, String language) {
        MallConfig old = mallConfigService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallConfigResponse response = BeanUtil.copyProperties(old, MallConfigResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
    }

    @Override
    public ReturnResult<List<MallConfigResponse>> all(String language) {
        List<MallConfig> list = mallConfigService.findList(null);
        if(null == list || 0 == list.size()){
            return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), new ArrayList<>());
        }
        List<MallConfigResponse> result = CopyUtils.copyList(list, MallConfigResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), result);
    }

    @Override
    public ReturnResult<MallConfigResponse> getConfigByCurrency(String currency, String language) {
        MallConfig old = mallConfigService.findByCurrency(currency);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallConfigResponse response = BeanUtil.copyProperties(old, MallConfigResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
    }
}
